# -*- coding: utf-8 -*-
"""
@time：  2021/05/23  13:34
@Auto：Mr. xingye  993843278
@Company 星野训练营 99384378@qq.com
@Function:excel数据驱动

"""
import os

import pytest

from common.excel.Excel import get_writer,get_reader
from keywords_App.appkeys import APP

class Coof:
    """数据驱动运行类"""
    def __init__(self):
        self.feature = ''
        self.feature_idx = 0
        self.story =''
        self.story_idx = 0
        # 保存所有的数据列表
        self.cases = []
        # 初始化关键字对象
        self.app =APP()
        #处理结果被覆盖
        self.s_idx = 1
        self.c_idx = 1



    def runCase(self,case_file='',res_file = ''):
        """获取excel数据，处理成列表，并且运行"""
        reader = get_reader(case_file)
        # writer = get_writer(case_file, res_file)


        #一个sheet页，一个sheet页去读
        sheetname = reader.get_sheets()
        print(sheetname)
        for sheet in sheetname:
            reader.set_sheet(sheet)
            #设置大分组名字
            self.feature = sheet
            self.feature_idx +=1
            self.story_idx = 0
            lines = reader.readline()

            #统计用例的步骤
            case = []
            # 遍历行
            for i in range(1,len(lines)):
                line = lines[i]
                #如果是模块
                if len(line[0])>1:
                    # 如果已经统计完了一个用例
                    if case:
                        self.cases.append(case)
                        #执行
                        self.__run_pytest()
                        case = []

                    #记录模块名字
                    self.story = line[0]
                    self.story_idx +=1
                # 如果是用例
                elif len(line[1]) >1:
                    if case:
                        self.cases.append(case)
                        self.__run_pytest()
                    #重置case
                    case = []
                    case.append(line)
                # 如果是步骤
                else:
                    #把步骤添加在case里面
                    case.append(line)

            # 当一个sheet读完，保存和运行最后一个用例
            self.cases.append(case)

            if self.cases:
                self.__run_pytest()


            self.s_idx += 1

        #最后把运行代码名字还原
        os.rename("./ddt/App_ddt/test_%s_%s.py" % (self.s_idx-1, self.c_idx - 1), "ddt/App_ddt/test_app.py")
        #关闭浏览器
        confApp.app.quit()

    def __run_pytest(self):
        """执行用例"""
        print("执行")
        print(self.cases)

        if self.c_idx ==1:
            os.rename("./ddt/App_ddt/test_app.py","./ddt/App_ddt/test_%s_%s.py" % (self.s_idx,self.c_idx))
        else:
            try:
                os.rename("./ddt/App_ddt/test_%s_%s.py" % (self.s_idx, self.c_idx-1),"./ddt/App_ddt/test_%s_%s.py" % (self.s_idx, self.c_idx))
            except:
                try:
                    os.rename("../ddt/App_ddt/test_%s_%s.py" % (self.s_idx-1, self.c_idx - 1),
                          "./ddt/App_ddt/test_%s_%s.py" % (self.s_idx, self.c_idx))
                except:
                    #处理下个多条excel同时执行，找不到模块的问题
                    os.rename("./ddt/App_ddt/test_web.py","./ddt/App_ddt/test_%s_%s.py" % (self.s_idx, self.c_idx))

        pytest.main(['-s',"./ddt/App_ddt/test_%s_%s.py" % (self.s_idx, self.c_idx), '--alluredir', 'temp'])

        self.c_idx += 1
        #清空cases
        self.cases.clear()




#使用全局变量传参
confApp = Coof()